<?php
/**
 * Created by PhpStorm.
 * User: iceberg
 * Date: 2018-10-20
 * Time: 10:41
 */

namespace app\service;



use app\model\Goods;
use think\facade\Request;

class GoodsService
{
    //分页
    public function page()
    {
        //获取request参数
        $request = Request::get();
        $name = isset($request['name']) ? $request['name'] : '';
        $status = isset($request['status']) ? $request['status'] : -1;
        //封装where查询条件
        $where = [];
        $status == -1   ||  $where[]   = ['Goods.status', '=' ,$status];
        //empty($name)    ||  $where[]   = ['name', 'like', '%'.$name.'%'];
        //拼接page参数
        $pageParam = ['query' => []];
        $pageParam['query']['status'] = $status;
        $pageParam['query']['name'] = $name;
        return Goods::hasWhere('category',function($query)use($name){
            if(!empty($name)){
                $query->where('name', 'like', '%'.$name.'%');
            }
        })->with('supplier,category,unit,storage')->where($where)->order('id desc')->paginate(10, false, $pageParam);
    }

    //统计个数
    public function count()
    {
        return Goods::count();
    }

}